package com.health.viewscontrols.tupian;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.health.R;
import com.health.viewscontrols.NovaActivity;

import java.io.File;

import static com.health.tools.GlobalConts.closeProgressDialog;

/**
 * 裁剪页面
 */
public class ClipActivity1 extends NovaActivity {
	private ClipImageLayout1 mClipImageLayout;
	public String path1;
	Button btn_back;//返回
	Button btn_up;//保存
	TextView title;//标题
//	Bitmap bit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_clipimage1);
		findViews();
		setListeners();
		try {

			// 这步必须要加
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			path1 = getIntent().getStringExtra("str_img");
			if (TextUtils.isEmpty(path1) || !(new File(path1).exists())) {
				Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
				return;
			}
			WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

			int width = wm.getDefaultDisplay().getWidth();
			int height = wm.getDefaultDisplay().getHeight();
			Bitmap bitmap = ImageUtil.comp(ImageUtil.convertToBitmap(path1,width- width/5, height-height/5), 500);
			int degree = ImageUtil.readPictureDegree(path1);
			bitmap = ImageUtil.rotaingImageView(degree, bitmap);
			if (bitmap == null) {
				Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
				return;
			}

//			bit = bitmap;
			mClipImageLayout = (ClipImageLayout1) findViewById(R.id.id_clipImageLayout);
			mClipImageLayout.setBitmap(bitmap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void findViews() {
		btn_back=findViewById(R.id.btn_back);
		btn_up=findViewById(R.id.btn_up);
		title=findViewById(R.id.title);
		title.setText("裁剪图片");
	}

	@Override
	public void setListeners() {
		btn_up.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showProgressDialog("正在处理");
				new Thread(new Runnable() {
					@Override
					public void run() {
						Bitmap bitmap = mClipImageLayout.clip();
						String path =path1;

						ImageUtil.savePhotoToSDCard(bitmap, path);
						cancelProgressDialog();
						Intent intent = new Intent();
						intent.putExtra("path", path);
						System.out.println("path" + path);
						setResult(RESULT_OK, intent);
//						AddCustomerMedicalActivity.setImg(path);
						finish();
					}
				}).start();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

	}



	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}
